package io.github.eric.urpc.standalone.server;

import io.github.eric.urpc.core.rpc.RpcContext;
import io.github.eric.urpc.core.rpc.RpcMethod;
import io.github.eric.urpc.core.rpc.RpcController;
import io.github.eric.urpc.standalone.common.msg.BusinessRequest;
import io.github.eric.urpc.standalone.common.msg.BusinessResponse;
import io.github.eric.urpc.standalone.common.msg.PingMessage;
import io.github.eric.urpc.standalone.common.msg.PongMessage;
import io.github.eric.urpc.standalone.common.msg.TestMessage;
import io.github.eric.urpc.standalone.common.msg.UserLogin;
import io.github.eric.urpc.standalone.common.msg.IMMessage;
import io.github.eric.urpc.standalone.common.msg.IMMessageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServerRpcController implements RpcController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServerRpcController.class);

    @RpcMethod
    public TestMessage onTestMessage(TestMessage testMessage) {
        LOGGER.info("server recv a test msg");
        return new TestMessage();
    }

    @RpcMethod
    public PongMessage onPingMessage(PingMessage pingMessage) {
        LOGGER.info("server recv a ping, send pong to client");
        return new PongMessage();
    }

    @RpcMethod
    public BusinessResponse onBusinessRequest(BusinessRequest businessRequest) {
        LOGGER.info("server recv a business request, send response to client");
        return new BusinessResponse();
    }

    @RpcMethod
    public void onRegisterUser(UserLogin userLogin, RpcContext rpcContext) {
        UserManager.getInstance().addUser(userLogin.getUserId(), userLogin.getUserName(), rpcContext.getConnId());
    }

    @RpcMethod
    public IMMessageResult onIMMessage(IMMessage imMessage) {
        return ChatManager.getInstance().sendMessage(imMessage);
    }

    @RpcMethod
    public void onIMMessage(IMMessageResult imMessageResult) {
        ChatManager.getInstance().relayIMMessageResult(imMessageResult);
    }
}
